package com.dawn.usermanage.serviceimpl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dawn.trademanage.bean.TradeInfoBean;
import com.dawn.trademanage.service.TradeManageService;
import com.dawn.usermanage.bean.UserBean;
import com.dawn.usermanage.dao.UserManageDao;
import com.dawn.usermanage.service.UserManageService;
import com.dawn.utils.exceptions.DaoAccessException;

@Service
public class UserManageServiceImpl implements UserManageService {

	private static Logger logger = Logger
			.getLogger(UserManageServiceImpl.class);

	@Autowired
	private UserManageDao userManageDao;

	@Autowired
	private TradeManageService tradeManageService;

	public UserBean findUserByUID(long userId) {
		return userManageDao.findUserByUid(userId);
	}

	public UserBean findUserBySinaId(String sinaId) {
		List<UserBean> users = null;
		UserBean user = null;
		try {
			users = userManageDao.findUserBySId(sinaId);
			if (!users.isEmpty()) {
				user = users.get(0);
			}
		} catch (Exception e) {
			logger.error("Data access error");
			e.printStackTrace();
			throw new DaoAccessException();
		}
		return user;
	}

	public boolean addUser(UserBean userBean) {
		try {
			TradeInfoBean tradeInfo = new TradeInfoBean();
			tradeInfo.setUnitPrice(5);
			tradeInfo.setCreditRate(6);
			tradeInfo.setBalance(30);
			tradeManageService.addTrade(tradeInfo);
			userBean.setTtTradeinfo(tradeInfo);
			userManageDao.addUser(userBean);
			return true;
		} catch (Exception e) {
			logger.error("Data access error");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

	public boolean updateUser(UserBean userBean) {
		try {
			userManageDao.uppdateUser(userBean);
			return true;
		} catch (Exception e) {
			logger.error("Data access error");
			e.printStackTrace();
			throw new DaoAccessException();
		}
	}

}
